﻿using System.Linq;
using QDT.Common;
using QDT.Common.Data;
using QDT.Core;
using QDT.Core.Domain;
using QDT.Core.Members;
using QDT.Payment;
using Quartz;

namespace QDT.Web.Admin.Code.Job
{
    public class CarryQueryJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            var dbProvider = IoC.Resolve<IUnitOfWork>() as IDbProvider;

            if (dbProvider == null)
            {
                return;
            }

            var carries = dbProvider.D<UserCarry>().Where(x => x.Status == 6);

            var payment = IoC.Resolve<IPayment>();

            foreach (var carry in carries)
            {
                var result = payment.CarryQueryRequest("C" + carry.ID);
                if (result == 1)
                {
                    carry.SetStatus(1);
                    var user = dbProvider.GetByID<User>(carry.UserId);
                    var log = user.DeductLockMoney(UserTransLog.TradeType.CarrayLockMoneyDefray, carry.Money, "", "提现成功，冻结金扣除");
                    dbProvider.Add(log);
                }
                else if (result == -1)
                {
                    carry.SetStatus(-1);
                }
            }

            dbProvider.SaveChanges();

        }
    }
}